/******************************************************************************
* @file    i2c.c
* @author  
* @version 
* @date   
* @brief   This file contains all the functions for the I2C peripheral.
*******************************************************************************
* @attention
*
******************************************************************************/

/* Includes ------------------------------------------------------------------*/
#include "drivers.h"

/* Private typedef -----------------------------------------------------------*/

/* Private define ------------------------------------------------------------*/

/* Private macro -------------------------------------------------------------*/

/* Private variables ---------------------------------------------------------*/

/* Private function prototypes -----------------------------------------------*/

/* Private functions ---------------------------------------------------------*/

/* Public functions ----------------------------------------------------------*/


/******************************************************************************
* @function         : 
* @description     :   
* @Algorithm 	      : 
* @input              : 
* @output           : 
* @Vision        
*   	date   	:	
*   	author  	:
*   	detail  	:
******************************************************************************/ 
void InitUSART(u32 baud)
{
	/* enable UART2 */
	//CLK_PeripheralClockConfig(CLK_PCKENR1_UART1, ENABLE);
	/* Configure UART2 */
	theUART_Init(baud, theUART_WORDLENGTH_8D, theUART_STOPBITS_1, theUART_PARITY_NO,theUART_SYNCMODE_CLOCK_DISABLE, theUART_MODE_TXRX_ENABLE);
}

/******************************************************************************
* @function         : 
* @description     :   
* @Algorithm 	      : 
* @input              : 
* @output           : 
* @Vision        
*   	date   	:	
*   	author  	:
*   	detail  	:
******************************************************************************/ 
void sendUSART(unsigned char c)
{
	/* fill buffer */
	while (!(theUART->SR & theUART_SR_TXE));
	theUART->DR = c;	
}

char putchar (char c)
{	
	if ( c != '\0' )
	{
	   sendUSART(c);
	}
	
	 return (c);
}
/******************************************************************************
* @function         : 
* @description     :   
* @Algorithm 	      : 
* @input              : 
* @output           : 
* @Vision        
*   	date   	:	
*   	author  	:
*   	detail  	:
******************************************************************************/ 
#ifdef USART_RECIEVE_CHECK_TIMEOUT
u8 receiveUSART(unsigned char* c, unsigned int time)
{
		while ( time-- )
    {
        if ( __bit(theUART->SR).b5 )
        {
            *c = (unsigned char)theUART->DR;
            return 1;
        }
    }
    return 0;
}
#else
u8 receiveUSART(unsigned char* c)
{
		if ( __bit(theUART->SR).b5 )
    {
        *c = (unsigned char)theUART->DR;
        return 1;
    }
    return 0;
}
#endif /*end USART_RECIEVE_CHECK_TIMEOUT*/

/******************************************************************************
* @function         : 
* @description     :   
* @Algorithm 	      : 
* @input              : 
* @output           : 
* @Vision        
*   	date   	:	
*   	author  	:
*   	detail  	:
******************************************************************************/ 
void Printf_USART(unsigned char * str)
{
	while( *str != '\0' )
	{
		sendUSART(*str);	
		str ++;
	}
}